﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.StockCountServiceRef;

    public class PCReview : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnSave;
        protected CheckBox chbOnlyNonEmptyLoc;
        protected CheckBox chbShowSystemQty;
        protected DropDownList ddlStatus;
        protected HtmlForm F5561;
        protected GridView gvPcMainTenanceDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnPrID;
        protected HtmlInputHidden hdnTagNo;
        protected HtmlInputButton htmlbtnReport;
        protected Label lblAreaIDEnd;
        protected Label lblAreaIDStart;
        protected Label lblDescr;
        protected Label lblEnd;
        protected Label lblEndingTagNo;
        protected Label lblLocEnd;
        protected Label lblLocLevelEnd;
        protected Label lblLocLevelStart;
        protected Label lblLocStart;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblOwnerEnd;
        protected Label lblOwnerStart;
        protected Label lblPhysicalRequestID;
        protected Label lblPlanEndTime;
        protected Label lblPlanStartTime;
        protected Label lblSkuEnd;
        protected Label lblSkuStart;
        protected Label lblStart;
        protected Label lblStartingTagNo;
        protected Label lblStatus;
        protected Label lblZoneIDEnd;
        protected Label lblZoneIDStart;
        protected LinkButton lnkbtnAdjustPCReview;
        protected LinkButton lnkbtnPcRequestTasks;
        protected LinkButton lnkbtnPrintPCReview;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnAdjustPCReview;
        protected HtmlTableCell tdlnkbtnPcRequestTasks;
        protected HtmlTableCell tdlnkbtnPrintPCReview;
        protected TextBox txtAreaIDEnd;
        protected TextBox txtAreaIDStart;
        protected TextBox txtDescr;
        protected TextBox txtEndingTagNo;
        protected TextBox txtLocEnd;
        protected TextBox txtLocLevelEnd;
        protected TextBox txtLocLevelStart;
        protected TextBox txtLocStart;
        protected TextBox txtOwnerEnd;
        protected TextBox txtOwnerStart;
        protected TextBox txtPhysicalRequestID;
        protected TextBox txtPlanEndTime;
        protected TextBox txtPlanStartTime;
        protected TextBox txtSkuEnd;
        protected TextBox txtSkuStart;
        protected TextBox txtStartingTagNo;
        protected TextBox txtZoneIDEnd;
        protected TextBox txtZoneIDStart;
        protected UpdatePanel up1;

        protected void btnAdjustPCReview_Click(object sender, EventArgs e)
        {
            string temp = string.Empty;
            if (this.ddlStatus.SelectedValue.ToUpper() == "CLOSED")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5780_009");
            }
            else if (this.ddlStatus.SelectedValue.ToUpper() != "RECOUNTED")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5780_010");
            }
            else if (!string.IsNullOrEmpty(this.txtPhysicalRequestID.Text))
            {
                temp = StockCountServiceClientProxy.GetInstance().AdjustInventoryQtyForPc(this.txtPhysicalRequestID.Text);
                if (string.IsNullOrEmpty(temp))
                {
                    this.lblMsg.Text = this.txtPhysicalRequestID.Text + base.GetGlobalMsgResource("F5780_011");
                    this.SetPhysicalRequestInfo2Form();
                }
                else
                {
                    this.lblMsg.Text = temp;
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5780_012");
                this.lblMsg.Visible = true;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            if (this.hdnFrom.Value == "list")
            {
                base.Response.Redirect("PCReviewList.aspx?from=edit", false);
            }
            else
            {
                base.Response.Redirect("PCReviewQuery.aspx", false);
            }
        }

        protected void btnPcRequestTasks_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue.ToUpper() == "CLOSED")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5780_005");
            }
            else if (this.ddlStatus.SelectedValue.ToUpper() != "COUNTED")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5780_006");
                this.lblMsg.Visible = true;
            }
            else if (!string.IsNullOrEmpty(this.txtPhysicalRequestID.Text))
            {
                if (!string.IsNullOrEmpty(OutboundServiceClientProxy.GetInstance().ReleasePcTasks(new List<string> { this.txtPhysicalRequestID.Text }, base.UserInfo.UserID, "RCReleased")))
                {
                    this.SetPhysicalRequestInfo2Form();
                    this.lblMsg.Text = this.txtPhysicalRequestID.Text + base.GetGlobalMsgResource("F5780_007");
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5780_008");
                this.lblMsg.Visible = true;
            }
        }

        protected void btnSave_Click(object sener, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() == "CLOSED")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5780_001");
                }
                else
                {
                    List<PhysicalTagInfo> physicalTagList = new List<PhysicalTagInfo>();
                    PhysicalTagInfo physicalTagInfo = null;
                    for (int i = 0; i < this.gvPcMainTenanceDetail.Rows.Count; i++)
                    {
                        if (((CheckBox) this.gvPcMainTenanceDetail.Rows[i].FindControl("chbSelect")).Checked)
                        {
                            if ((!string.IsNullOrEmpty(this.gvPcMainTenanceDetail.DataKeys[i]["STATUS"].ToString()) && (this.gvPcMainTenanceDetail.DataKeys[i]["STATUS"].ToString().ToUpper() == "RECOUNTPRINTED")) || (this.gvPcMainTenanceDetail.DataKeys[i]["STATUS"].ToString().ToUpper() == "RCRELEASED"))
                            {
                                physicalTagInfo = new PhysicalTagInfo {
                                    PhysicalRequestID = this.gvPcMainTenanceDetail.DataKeys[i]["PHYSICALREQUESTID"].ToString(),
                                    TagNo = Convert.ToInt32(this.gvPcMainTenanceDetail.DataKeys[i]["TAGNO"]),
                                    Status = "Recounted",
                                    RecountBy = base.UserInfo.UserID,
                                    RecountDate = DateTime.Now
                                };
                                if (!string.IsNullOrEmpty(((TextBox) this.gvPcMainTenanceDetail.Rows[i].FindControl("txtRecountQty")).Text))
                                {
                                    physicalTagInfo.SysQty = decimal.Parse(this.gvPcMainTenanceDetail.DataKeys[i]["SYSQTY"].ToString());
                                    physicalTagInfo.RecountQty = decimal.Parse(((TextBox) this.gvPcMainTenanceDetail.Rows[i].FindControl("txtRecountQty")).Text);
                                    physicalTagInfo.AdjQty = physicalTagInfo.RecountQty - physicalTagInfo.SysQty;
                                }
                                physicalTagList.Add(physicalTagInfo);
                            }
                            else
                            {
                                this.lblMsg.Text = base.GetGlobalMsgResource("F5780_002");
                                return;
                            }
                        }
                    }
                    if (physicalTagList.Count > 0)
                    {
                        string strValue = StockCountServiceClientProxy.GetInstance().UpdatePhysicalTagList(physicalTagList, "RECOUNT");
                        this.lblMsg.Text = base.GetGlobalMsgResource("F5780_003") + strValue + base.GetGlobalMsgResource("F5780_004");
                        this.LoadData();
                        ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvPcMainTenanceDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5561", this.gvPcMainTenanceDetail);
        }

        protected void gvPcMainTenanceDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as PhysicalRequestInfo).PhysicalTagInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvPcMainTenanceDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                ((TextBox) e.Row.FindControl("txtRecountQty")).Attributes.Add("onblur", "CountQty(this,'" + e.Row.FindControl("lblSysQty").ClientID + "','" + e.Row.FindControl("lblAdjQty").ClientID + "'); CheckTrue('" + e.Row.FindControl("chbSelect").ClientID + "',this,'" + ((TextBox) e.Row.FindControl("txtRecountQty")).Text + "');");
            }
        }

        private void InsertPhysicalRequest()
        {
            try
            {
                string PrID = string.Empty;
                PhysicalRequestInfo physicalRequestInfo = new PhysicalRequestInfo {
                    PhysicalRequestID = this.txtPhysicalRequestID.Text,
                    Status = this.ddlStatus.SelectedValue,
                    Descr = this.txtDescr.Text
                };
                if (!string.IsNullOrEmpty(this.txtStartingTagNo.Text))
                {
                    physicalRequestInfo.StartingTagNo = Convert.ToInt32(this.txtStartingTagNo.Text);
                }
                physicalRequestInfo.PlanStartTime = Convert.ToDateTime(this.txtPlanStartTime.Text);
                physicalRequestInfo.PlanEndTime = Convert.ToDateTime(this.txtPlanEndTime.Text);
                physicalRequestInfo.OwnerIdStart = this.txtOwnerStart.Text;
                physicalRequestInfo.OwnerIdEnd = this.txtOwnerEnd.Text;
                physicalRequestInfo.SkuIdStart = this.txtSkuStart.Text;
                physicalRequestInfo.SkuIdEnd = this.txtSkuEnd.Text;
                physicalRequestInfo.LocStart = this.txtLocStart.Text;
                physicalRequestInfo.LocEnd = this.txtLocEnd.Text;
                if (!string.IsNullOrEmpty(this.txtLocLevelStart.Text))
                {
                    physicalRequestInfo.LocLevelStart = Convert.ToInt32(this.txtLocLevelStart.Text);
                }
                if (!string.IsNullOrEmpty(this.txtLocLevelEnd.Text))
                {
                    physicalRequestInfo.LocLevelEnd = Convert.ToInt32(this.txtLocLevelEnd.Text);
                }
                physicalRequestInfo.ZoneIdStart = this.txtZoneIDStart.Text;
                physicalRequestInfo.ZoneIdEnd = this.txtZoneIDEnd.Text;
                physicalRequestInfo.AreaIdStart = this.txtAreaIDStart.Text;
                physicalRequestInfo.AreaIdEnd = this.txtAreaIDEnd.Text;
                physicalRequestInfo.ShowSystemQty = this.chbShowSystemQty.Checked ? "Y" : "N";
                physicalRequestInfo.OnlyNonEmptyLoc = this.chbShowSystemQty.Checked ? "Y" : "N";
                physicalRequestInfo.CreatedBy = base.UserInfo.UserID;
                PrID = StockCountServiceClientProxy.GetInstance().InsertPhysicalRequest(physicalRequestInfo);
                if (!string.IsNullOrEmpty(PrID))
                {
                    this.hdnPrID.Value = PrID;
                    this.lblMsg.Text = PrID + base.GetGlobalMsgResource("C004");
                    this.lblMsg.Visible = true;
                    this.txtPhysicalRequestID.Text = PrID;
                    this.txtPhysicalRequestID.Enabled = false;
                    this.hdnTagNo.Value = "10";
                    this.hdnFlag.Value = "1";
                    base.DataEntityViewState = physicalRequestInfo;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "PCStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnPrID.Value = base.Request.QueryString["ID"].ToString();
                this.hdnFlag.Value = "1";
                this.SetPhysicalRequestInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one'," + base.Request.QueryString["detail"].ToString() + ",2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPhysicalRequestInfo2Form()
        {
            try
            {
                string PhysicalRequestID = this.hdnPrID.Value.ToString();
                PhysicalRequestInfo physicalRequestInfo = StockCountServiceClientProxy.GetInstance().GetPCReviewInfoByID(PhysicalRequestID);
                base.DataEntityViewState = physicalRequestInfo;
                if (physicalRequestInfo != null)
                {
                    this.txtPhysicalRequestID.Text = physicalRequestInfo.PhysicalRequestID;
                    this.txtPhysicalRequestID.Enabled = false;
                    this.ddlStatus.SelectedValue = physicalRequestInfo.Status;
                    this.ddlStatus.Enabled = false;
                    this.txtDescr.Text = physicalRequestInfo.Descr;
                    this.txtStartingTagNo.Text = physicalRequestInfo.StartingTagNo.ToString();
                    this.txtEndingTagNo.Text = "";
                    this.txtPlanStartTime.Text = base.GetPreferenceShortDate(physicalRequestInfo.PlanStartTime);
                    this.txtPlanEndTime.Text = base.GetPreferenceShortDate(physicalRequestInfo.PlanEndTime);
                    this.txtOwnerStart.Text = physicalRequestInfo.OwnerIdStart;
                    this.txtOwnerEnd.Text = physicalRequestInfo.OwnerIdEnd;
                    this.txtSkuStart.Text = physicalRequestInfo.SkuIdStart;
                    this.txtSkuEnd.Text = physicalRequestInfo.SkuIdEnd;
                    this.txtLocStart.Text = physicalRequestInfo.LocStart;
                    this.txtLocEnd.Text = physicalRequestInfo.LocEnd;
                    this.txtLocLevelStart.Text = physicalRequestInfo.LocLevelStart.ToString();
                    this.txtLocLevelEnd.Text = physicalRequestInfo.LocLevelEnd.ToString();
                    this.txtZoneIDStart.Text = physicalRequestInfo.ZoneIdStart;
                    this.txtZoneIDEnd.Text = physicalRequestInfo.ZoneIdEnd;
                    this.txtAreaIDStart.Text = physicalRequestInfo.AreaIdStart;
                    this.txtAreaIDEnd.Text = physicalRequestInfo.AreaIdEnd;
                    this.chbShowSystemQty.Checked = physicalRequestInfo.ShowSystemQty != "N";
                    this.chbOnlyNonEmptyLoc.Checked = physicalRequestInfo.OnlyNonEmptyLoc != "N";
                    base.BindData2GridView(this.gvPcMainTenanceDetail, physicalRequestInfo.PhysicalTagInfos);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

